﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class KMrpt4 : BaseRptPage
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        this.Load += new System.EventHandler(Page_Load);
    } 
    protected  void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.txtsdate.Text = DateTime.Now.ToString("yyyy-MM-01");
            this.txtedate.Text = DateTime.Now.ToString("yyyy-MM-dd");
            //this.GridView1.RowDataBound += new GridViewRowEventHandler(this.GridView_RowDataBound);
            //this.GridView1.Sorting += new  GridViewSortEventHandler(this.GridView_Sorting);
        }
    }
    protected override void GvBind()
    {
        string sortedField = GetSortedFields();
        if (string.IsNullOrEmpty(sortedField))
        {
            sortedField = "";
        }
        this.GridView1.DataSource = db.DM_GetDateSet("KM_PurDetailBarCode '"+txtyear.Text+"','"+txtseason.Text
            +"','"+txtsdate.Text+"','"+txtedate.Text+"','"+txtvcode.Text+"','"+txtvname.Text+"','"+txtstyleno.Text+"','"+sortedField+"'");
        this.GridView1.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        GvBind();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        GvBind();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        //GridView1.AllowPaging = false;
        GvBind();
        GridViewToExcel(this.GridView1, "application/ms-excel", this.Title + ".xls");
        //GridView1.AllowPaging = true;
    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        this.GridView_Sorting(sender, e);
    }
    protected void GridView1_DataBound(object sender, EventArgs e)
    {
        int index = -1;
        int startGroupIndex = 0;
        if (GridView1.Rows.Count < 1) { return; }
        foreach (GridViewRow row in GridView1.Rows)
        { 
            index++;
            if (index > 0 && (row.Cells[0].Text.ToString().Trim() == "" || row.Cells[0].Text.ToString().Trim() == "&nbsp;")) 
            { 
                row.Cells[0].Text = row.Cells[0].Text+" 小计";// GridView1.Rows[startGroupIndex].Cells[0].Text; 
                row.Attributes.Add("style", "background-color: #F2F2F2; font-weight: bold;font-variant: normal; color: #000000;"); 
                row.Attributes.Remove("onmouseout");
                row.Attributes.Remove("onmouseover");
                GridViewGroup.GroupRow(GridView1, index, 0, 2);
                GridViewGroup.GropCell(GridView1, 0, startGroupIndex, index - 1);
                startGroupIndex = index + 1;
            }
        }
        GridViewRow sumRow = GridView1.Rows[index];
        sumRow.Attributes.Add("style", "background-color: #EAEAEA;font-weight: bold;font-variant: normal;color: #000000;");
        sumRow.Attributes.Remove("onmouseout");
        sumRow.Attributes.Remove("onmouseover");
        GridViewGroup.GroupRow(GridView1, index, 0, 2);
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            //判断是否进行排序
            if (ViewState["SortedField"] != null)
            {
                //恢复排序信息
                Dictionary<string, string> order = (Dictionary<string, string>)ViewState["SortedField"];
                //循环表头的单元格，判断是否有需要排序的单元格
                for (int i = 0; i < e.Row.Cells.Count; i++)
                {
                    if (e.Row.Cells[i].Controls.Count > 0)
                    {
                        //是否是排序字段信息
                        LinkButton lb = e.Row.Cells[i].Controls[0] as LinkButton;
                        if (lb != null)
                        {
                            //反序和降序
                            if (order.ContainsKey(lb.CommandArgument))
                            {
                                Literal li = new Literal();
                                if (order[lb.CommandArgument] == "ASC")
                                {
                                    li.Text = "▲";
                                }
                                else
                                {
                                    li.Text = "▼";
                                }
                                //
                                e.Row.Cells[i].Controls.Add(li);
                            }
                        }
                    }
                }
            }
        }
    }
}